package com.lancoo.ccas53.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lancoo.ccas53.entity.*;
import com.lancoo.ccas53.exception.BizException;
import com.lancoo.ccas53.mapper.CollegeMapper;
import com.lancoo.ccas53.mapper.PeriodSchemeMapper;
import com.lancoo.ccas53.pojo.common.ProhibitType;
import com.lancoo.ccas53.service.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class BasicRuleServiceImpl implements BasicRuleService {

    @Resource
    private RuleUsableService ruleUsableService;
    @Resource
    private CollegeMapper collegeMapper;
    @Resource
    private CollegeRoomService collegeRoomService;

    @Resource
    private RuleWeekDayService ruleWeekDayService;
    @Resource
    private PeriodSchemeMapper periodSchemeMapper;
    @Resource
    private RuleTimeProhibitService ruleTimeProhibitService;


    @Override
    public List<RuleUsable> getCurrent(Integer taskId, String schoolId, String term, String collegeId) {
        return ruleUsableService.lambdaQuery().eq(RuleUsable::getTaskId, taskId).list();
    }

    @Override
    public List<Long> getCollegeRoom(Integer taskId, String collegeId, String keyword) {
        List<CollegeRoom> collegeRoomList = collegeRoomService.lambdaQuery()
                .eq(CollegeRoom::getTaskId, taskId)
                .eq(CollegeRoom::getBaseCollegeId, collegeId).list();
        if (CollUtil.isEmpty(collegeRoomList)) {
            throw new BizException("院系未分配场地");
        }
        return collegeRoomList.stream().map(CollegeRoom::getRoomId).distinct().collect(Collectors.toList());
//        List<CollegeDto> collegeDtoList = collegeMapper.getCollegeByTaskId(taskId);
//        if (StrUtil.isNotBlank(keyword)) {
//            collegeDtoList = collegeDtoList.stream()
//                    .filter(collegeDto -> collegeDto.getCollegeName().contains(keyword)).collect(Collectors.toList());
//        }
//        if (StrUtil.isNotBlank(collegeId)) {
//            collegeDtoList = collegeDtoList.stream()
//                    .filter(collegeDto -> collegeDto.getCollegeId().equals(collegeId)).collect(Collectors.toList());
//        }
//        List<CollegeRoomVo> collegeRoomVoList = BeanUtil.copyToList(collegeDtoList, CollegeRoomVo.class);
//        collegeRoomVoList.forEach(o -> o.setBaseCollegeId());
//        if (CollUtil.isNotEmpty(collegeRoomVoList)) {
//            //添加上课场地
//            List<String> collegeIds = collegeRoomVoList.stream().map(CollegeRoomVo::getBaseCollegeId).collect(Collectors.toList());
//            collegeRoomService.lambdaQuery().eq(CollegeRoom::getTaskId, taskId).in(CollegeRoom::getBaseCollegeId, collegeIds).list().
//                    stream().collect(Collectors.groupingBy(CollegeRoom::getBaseCollegeId)).forEach((k, v) -> {
//                collegeRoomVoList.forEach(collegeRoomVo -> {
//                    if (collegeRoomVo.getBaseCollegeId().equals(k)) {
//                        collegeRoomVo.setCollegeRooms(v);
//                    }
//                });
//            });
//        }
//        return collegeRoomVoList;
    }

    @Override
    public List<RuleWeekDay> getWeekDays(Integer taskId) {
        List<RuleWeekDay> weekDays = ruleWeekDayService.lambdaQuery().eq(RuleWeekDay::getTaskId, taskId).list();
        //填充节次信息
        List<PeriodScheme> periodSchemes = periodSchemeMapper.selectList(new LambdaQueryWrapper<PeriodScheme>()
                .eq(PeriodScheme::getTaskId, taskId));
        //分组
        Map<String, List<PeriodScheme>> periodMap = periodSchemes.stream().collect(Collectors.groupingBy(PeriodScheme::getBaseSchemeId));
        weekDays.forEach(day -> {
            day.setPeriodSchemes(periodMap.get(day.getBaseSchemeId()));
        });
        return weekDays;
    }

    @Override
    public List<RuleTimeProhibit> getTimeProhibit(Integer taskId, String collegeId) {
        List<RuleTimeProhibit> timeProhibits = new ArrayList<>();
//        if (ObjUtil.isNull(collegeId)) {
//            return timeProhibits;
//        }
        timeProhibits = ruleTimeProhibitService.lambdaQuery()
                .eq(RuleTimeProhibit::getTaskId, taskId)
                .eq(RuleTimeProhibit::getOptionType, ProhibitType.TIME.getCode()).list();
        return timeProhibits;

    }


    @Override
    public int getMaxPeriod(Integer taskId) {


        //获取对应的课时方案
        List<String> baseSchemeId = ruleWeekDayService.lambdaQuery().eq(RuleWeekDay::getTaskId, taskId).list().stream()
                .map(RuleWeekDay::getBaseSchemeId).distinct().collect(Collectors.toList());


        final int[] max = {0};
        if (CollUtil.isNotEmpty(baseSchemeId)) {
            Map<String, List<PeriodScheme>> periodMap = periodSchemeMapper.selectList(new LambdaQueryWrapper<PeriodScheme>().eq(PeriodScheme::getTaskId, taskId)
                            .in(PeriodScheme::getBaseSchemeId, baseSchemeId)).stream()
                    .collect(Collectors.groupingBy(PeriodScheme::getBaseSchemeId));

            periodMap.forEach((k, v) -> {
                max[0] = Math.max(max[0], v.size());
            });
        }
        return max[0];
    }
}
